rawdata<-read.csv("C:/Users/Mittal/Desktop/Logistic_Regression_Raw_Data.csv")
attach(rawdata)
thedata<-data.frame(decdich, dj, pj, sg, age, tenure, educ, racedich, genddich)
thedata <- na.omit(thedata)
Labels<-names(thedata)[2:length(thedata)]

logRegress<-function(mydata){
numVar<<-NCOL(mydata)
Variables<<-names(mydata)[2:numVar]

Y<-mydata[,1]
data.preds<-mydata[,2:length(mydata[1,])]
X<-scale(data.preds)

X.svd<-svd(X)
Q<-X.svd$v
P<-X.svd$u
Z<-P%*%t(Q)

Z.stand<-scale(Z)

Lambda<-solve(t(Z.stand)%*%Z.stand)%*%t(Z.stand)%*%X #Obtaining Lambda from equation 7 from Johnson (2000) pg 8

logrfit<-glm(Y~Z.stand,family=binomial)
unstCoefs<-coef(logrfit)
b<-unstCoefs[2:length(unstCoefs)]
LpredY<-predict(logrfit,newdata=mydata,type="response")
lYhat<-log(LpredY/(1-LpredY))#Creating logit-Y-hat
stdlYhat<-sd(lYhat)#Getting stdev of logit-Y-hat
getting.Rsq<-lm(LpredY~Y)#Getting R-sq
Rsq<-summary(getting.Rsq)$r.squared
beta<-b*((sqrt(Rsq))/stdlYhat)#Computing standardized logistic regression coefficients

epsilon<-Lambda^2%*%beta^2
R.sq<<-sum(epsilon)
PropWeights<-(epsilon/R.sq)
result<<-data.frame(Variables, Raw.RelWeight=epsilon, Rescaled.RelWeight=PropWeights) 
}


logBootstrap<-function(mydata, indices){
	mydata<-mydata[indices,]
	logWeights<-logRegress(mydata)
	return(logWeights$Raw.RelWeight)
}

logBootrand<-function(mydata, indices){
	mydata<-mydata[indices,]
	logRWeights<-logRegress(mydata)
	logReps<-logRWeights$Raw.RelWeight
	randWeight<-logReps[length(logReps)]
	randStat<-logReps[-(length(logReps))]-randWeight
	return(randStat)
}

#bootstrapping
install.packages("boot")
library(boot)

mybootci<-function(x){
	boot.ci(logBoot,conf=0.95, type="bca", index=x)
}

runBoot<-function(num){
	INDEX<-1:num
	test<-lapply(INDEX, FUN=mybootci)
	test
	test2<-t(sapply(test,'[[',i=4)) #extracts confidence interval
	CIresult<<-data.frame(Variables, CI.Lower.Bound=test2[,4],CI.Upper.Bound=test2[,5])
}
myRbootci<-function(x){
	boot.ci(logRBoot,conf=0.95,type="bca",index=x)
}

runRBoot<-function(num){
	INDEX<-1:num
	test<-lapply(INDEX,FUN=myRbootci)
	test2<-t(sapply(test,'[[',i=4))
CIresult<<-data.frame(Labels,CI.Lower.Bound=test2[,4],CI.Upper.Bound=test2[,5])
}



logRegress(thedata)
RW.Results<-result

RSQ.o<-R.sq


#Bootstrap Confidence interval around the individual relative weights
#Please be patient -- This can take a few minutes to run
logBoot<-boot(thedata, logBootstrap, 10000)
logci<-boot.ci(logBoot,conf=0.95, type="bca")
runBoot(length(thedata[,2:numVar]))
CI.Results<-CIresult

#Bootstrapped Confidence interval tests of Significance
#Please be patient -- This can take a few minutes to run
randVar<-rnorm(length(thedata[,1]),0,1)
randData<-cbind(thedata,randVar)
logRBoot<-boot(randData,logBootrand, 10000)
logRci<-boot.ci(logRBoot,conf=0.95, type="bca")
runRBoot(length(randData[,2:(numVar-1)]))
CI.Significance<-CIresult


#Rsq.O - logistic analog to Rsq
RSQ.o


#The Raw and Rescaled Weights
RW.Results
#BCa Confidence Intervals around the raw weights
CI.Results
#BCa Confidence Interval Tests of significance
#If Zero is not included, Weight is Significant
CI.Significance



